﻿namespace MyCompany.ViewModels {
	using System;
	using System.Windows;

	using SolidMvvm;

	using SolidMvvmInterfaces;

	public class LogViewModel : ViewModelBase {
		public delegate void OKRequestedDelegate(LogViewModel sender);

		public LogViewModel(string log, IMessageReporter messageReporter, OKRequestedDelegate okRequestedDelegate) : base(messageReporter) {
			try {
				Log = log;
				OKRequested = okRequestedDelegate;
			}
			catch (Exception exception) {
				Cleaner.CleanConstructor(this, exception);
			}
		}

		public ILogViewModelObserver LogViewModelObserver { get; set; }

		public OKRequestedDelegate OKRequested { get; private set; }

		public string Log { get; private set; }

		public override void Dispose() {
			using (var cleaner = new Cleaner()) {
				cleaner.CleanWithLambda(() => LogViewModelObserver?.DisconnectFromViewModel());

				// Set fields to null, -1, double.NaN, etc. to render the object useless.
				Log = null;
				OKRequested = null;

				cleaner.CleanWithLambda(() => base.Dispose());
			}
		}

		public void _CopyToClipboard() {
			Clipboard.SetData(DataFormats.Text, Log);
		}

		public void _OKRequested() {
			OKRequested(this);
		}

		public interface ILogViewModelObserver : IViewModelObserver {}
	}
}
